<template>
  <div>
    <h3>注册页面</h3>
    <el-form :model="ruleUser" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" style="width:500px;margin:0 auto">
        <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleUser.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleUser.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="password2">
            <el-input type="password" v-model="ruleUser.password2" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
            <el-input v-model="ruleUser.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
            <el-input v-model="ruleUser.code" autocomplete="off" style="width:60%"></el-input>
            <div class="image">
                <img :src="imageUrl" alt="图片验证码" @click="image" style="height:60px">
            </div>
        </el-form-item>

        <el-form-item prop="agree">
            <el-checkbox v-model="ruleUser.agree">请勾选用户协议</el-checkbox>
        </el-form-item>
      
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
            <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
        </el-form-item>
</el-form>

  </div>
</template>

<script>
import {v4 as uuid4} from 'uuid'
import axios from 'axios'
  export default {
    data() {
        var validateUser = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入用户名'));
        } 
        const userRules=/^[a-zA-Z][a-zA-Z0-9_]\w{4,15}$/
        if(userRules.test(value)){
            axios.get('user/checkuser/'+value+'/')
            .then(resp=>{
                console.log(resp);
                if(resp.data.code == 200){
                    return callback()
                }else{
                    return callback(resp.data.msg)
                }
            })
            .catch(err=>{
                console.log(err);
            })
        }else{
           return callback(new Error('用户名格式错误'));
        }
      };

      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } 
        const passRules=/^[a-zA-Z][a-zA-Z0-9]{4,15}$/
        if(passRules.test(value)){
            return callback();
        }else{
            return callback(new Error('密码格式错误'));
        }
      };
      var validatePassword = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleUser.password2) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };

      var validateMobile = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入手机号'));
        } 
        const userRules=/^1[3-9][0-9]{9}$/
        if(userRules.test(value)){
            axios.get('user/checkmobile/'+value+'/')
            .then(resp=>{
                console.log(resp);
                if(resp.data.code == 200){
                    return callback()
                }else{
                    return callback(resp.data.msg)
                }
            })
            .catch(err=>{
                console.log(err);
            })
        }else{
           return callback(new Error('手机号格式错误'));
        }
      };
      var validateImage = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入验证码'));
        } 
        axios.post('user/checkimage/'+this.imageId+'/',{code:value})
            .then(resp=>{
                console.log(resp);
                if(resp.data.code == 200){
                    return callback()
                }else{
                    return callback(new Errow(resp.data.msg))
                }
            })
            .catch(err=>{
                console.log(err);
            })
      };
      return {
        imageUrl:'',
        imageId:'',
        ruleUser: {
          username:'',
          password: '',
          password2: '',
          mobile: '',
          code:'',
          agree:false
        },
        rules: {
          username: [
            { validator: validateUser, trigger: 'blur' }
          ],
          password: [
            { validator: validatePass, trigger: 'blur' }
          ],
          password2: [
            { validator: validatePassword, trigger: 'blur' }
          ],
          mobile: [
            { validator: validateMobile, trigger: 'blur' }
          ],
          code: [
            { validator: validateImage, trigger: 'blur' }
          ],
         
         
        }
      };
    },
    mounted(){
        this.image()
    },
    methods: {
      image(){
        this.imageId=uuid4()
        this.imageUrl="user/checkimage/"+this.imageId+'/'
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            axios.post('user/register/',this.ruleUser)
            .then(resp=>{
                console.log(resp);
                if(resp.data.code == 200){
                    alert('注册成功')
                    this.$router.push('/login')
                }
            })
            .catch(err=>{
                console.log(err);
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style>
.image{
    float: right;
    width: 30%;
}
</style>